// import axios from '/static/js/axios'
// import { Loading, Message } from '/static/js/element-ui'

// let loading
function startLoading() {
    // loading = Loading.service({
    //     lock: true,
    //     text: '加载中....',
    //     background: 'rgba(0, 0, 0, 0.7)'
    // })
}
axios.defaults.headers.post['Content-Type'] = 'application/json; charset=utf-8';
function endLoading() {
    // loading.close()
}

// 请求拦截
axios.interceptors.request.use(
    (confing) => {
        startLoading()

        //设置请求头
        if (localStorage.eToken) {
            confing.headers.Authorization = localStorage.eToken
        }

        // if (confing.method==="post"){
        //     confing.headers.ContentType = 'application/json'
        // }
        return confing
    },
    (error) => {
        return Promise.reject(error)
    }
)

//响应拦截

axios.interceptors.response.use(
    (response) => {
        if (response.status === 200){
            if (response.data.code===0) {
                endLoading()
                return response.data;
            }else {
                return Promise.reject(response)
            }
        }else {
           return  Promise.reject(response)
        }

    },
    (error) => {
        // Message.error(error.response.data)
        endLoading()

        // 获取状态码
        const { status } = error.response

        if (status === 401) {
            // Message.error('请重新登录')
            //清楚token
            localStorage.removeItem('eToken')
            //跳转到登录页面
            // router.push('/login')
        }
        return Promise.reject(error)
    }
)


// export default axios;